//
//  DIYdetailViewModel.m
//  BigJoy
//
//  Created by Rowling on 2017/6/22.
//  Copyright © 2017年 Rowling. All rights reserved.
//

#import "DIYdetailViewModel.h"
#import "JFImagePickerController.h"
#import "XMtoolView.h"
#import "TextColorView.h"
#import "FontNameSelectView.h"

typedef enum {
TextFamliy = 2,
TextColor = 1,
KeyBoard = 0
}ToolType;

@interface DIYdetailViewModel()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic,weak)UIView * VC;
@property (nonatomic,strong) XMtoolView *toolView;
@property (nonatomic,strong) TextColorView *textColorView;
@property (nonatomic,strong) FontNameSelectView *fontSelectView;
@property (nonatomic,assign) ToolType toolType;

@end

@implementation DIYdetailViewModel

-(TextColorView *)textColorView {
    if (_textColorView == nil) {
        _textColorView = [TextColorView fastLogin];
        XMweakSelf;
        [_textColorView.xmcolorVIew setColorBlock:^(UIColor * color){
            weakSelf.cureentView.XMtextView.textColor = color;
        }];
    }
    return _textColorView;
}

-(FontNameSelectView *)fontSelectView {
    if (_fontSelectView == nil) {
        _fontSelectView = [[FontNameSelectView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - XMH(150), SCREEN_WIDTH, XMH(150))];
        XMweakSelf;
        [_fontSelectView setFontSelectBlock:^(NSString * str){
            weakSelf.cureentView.fontName = str;
        }];
    }
    return _fontSelectView;
}

-(void)bindVC:(UIView *)VC {
    self.VC = VC;
}


-(RACSubject *)subject {
    if (_subject == nil) {
        _subject = [[RACSubject alloc]init];
    }
    return _subject;
}


#pragma -mark:上传图片或者拍照
- (void)imagePickerDidFinished:(JFImagePickerController *)picker{
    XMweakSelf;
    for (ALAsset *asset in picker.assets) {
        [[JFImageManager sharedManager] imageWithAsset:asset resultHandler:^(CGImageRef imageRef, BOOL longImage) {
            UIImage *image = [UIImage imageWithCGImage:imageRef];
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.subject sendNext:image];
//                [weakSelf.subject sendCompleted];
            });
        }];
    }
    [self imagePickerDidCancel:picker];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self imageHandleWithpickerController:picker MdediaInfo:info];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:^{}];
}

- (void)imageHandleWithpickerController:(UIImagePickerController *)picker MdediaInfo:(NSDictionary *)info {
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [self.subject sendNext:image];
//    [self.subject sendCompleted];
    [picker dismissViewControllerAnimated:YES completion:^{}];
}

- (void)imagePickerDidCancel:(JFImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
    [JFImagePickerController clear];
}

- (void)openPhotoFile
{
    NSInteger count = 1;
    [JFImagePickerController setMaxCount:count];
    JFImagePickerController *picker = [[JFImagePickerController alloc] initWithRootViewController:[UIViewController new]];
    picker.pickerDelegate = self;
    UIViewController * VC = [XMSuperHelper XMControllerOfView:self.VC];
    [VC.navigationController presentViewController:picker animated:YES completion:nil];
}

- (void)openCamera
{
    UIImagePickerController *vc = [UIImagePickerController new];
    vc.sourceType = UIImagePickerControllerSourceTypeCamera;//sourcetype有三种分别是camera，photoLibrary和photoAlbum
    vc.delegate = self;
    UIViewController * VC = [XMSuperHelper XMControllerOfView:self.VC];
    [VC.navigationController presentViewController:vc animated:YES completion:nil];
}

//addElement
-(NSMutableArray <XMbaseCanMoveView *>*)elementArray {
    if (_elementArray == nil) {
        _elementArray = [[NSMutableArray alloc]init];
    }
    return _elementArray;
}

-(NSMutableArray<TextFieldCanMoceView *> *)elementEditArray {
    if (_elementEditArray == nil) {
        _elementEditArray = [[NSMutableArray alloc]init];
    }
    return _elementEditArray;
}
// 关闭所有tap
-(void)editingEnabelTextViewTapCurrentTextView:(BOOL)enable {
    for (TextFieldCanMoceView * view in self.elementEditArray) {
        view.tap.enabled = enable;
        view.doubleTap.enabled = enable;
    }
    for (XMbaseCanMoveView * view in self.elementArray) {
        view.tap.enabled = enable;
    }
}

-(void)remoMoveView:(XMbaseCanMoveView *)View {
    if ([self.elementArray containsObject:View]) {
        [self.elementArray removeObject:View];
    }
    if ([self.elementEditArray containsObject:(TextFieldCanMoceView *)View]) {
        [self.elementEditArray removeObject:(TextFieldCanMoceView *)View];
    }
}

#pragma -mark:编辑的状态栏
int anlegmentInt = 0;
-(XMtoolView *)toolView {
    if (_toolView == nil) {
        _toolView = [XMtoolView fastLogin];
        _toolView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 50);
        _toolView.backgroundColor = [UIColor whiteColor];
        RACSubject * subject = [RACSubject subject];
        _toolView.subject = subject;
        @weakify(self);
        [subject subscribeNext:^(id  _Nullable x) {
            NSNumber * num = x;
            NSInteger tag = [num integerValue];
            @strongify(self);
            if (tag == 0) {
                self.toolType = TextFamliy;
                [self.VC endEditing:YES];
                [self addFontNameView];
            }
            if (tag == 1) {
                self.toolType = TextColor;
                [self.VC endEditing:YES];
                [self addTextColorView];
            }
            if (tag == 2) {
                [self changeAnlengmet];
            }
            if (tag == 3) {
                self.toolType = KeyBoard;
                [self changeToKeyBoard];
            }
        }];
        
        [self addobserverForKeyBorad];
    }
    return _toolView;
}
// 跳转到选择颜色
-(void)addTextColorView {
    self.cureentView.XMtextView.userInteractionEnabled = NO;
    [self.VC addSubview:self.textColorView];
    self.textColorView.frame = CGRectMake(0,SCREEN_HEIGHT - XMH(150), SCREEN_WIDTH, XMH(150));
    self.textColorView.backgroundColor = [UIColor yellowColor];
    [UIView animateWithDuration:0.2 animations:^{
        self.toolView.y = self.textColorView.y - self.toolView.height;
    }];
}
// 跳转到选择字体
-(void)addFontNameView {
    self.cureentView.XMtextView.userInteractionEnabled = NO;
    [self.VC addSubview:self.fontSelectView];
    [UIView animateWithDuration:0.2 animations:^{
        self.toolView.y = self.fontSelectView.y - self.toolView.height;
    }];
}

// 跳转到键盘
-(void)changeToKeyBoard {
    self.cureentView.XMtextView.userInteractionEnabled = YES;
    [self.cureentView.XMtextView becomeFirstResponder];
}

-(void)changeAnlengmet {
    anlegmentInt ++;
    if (anlegmentInt == 3) {
        anlegmentInt = 0;
    }
    self.cureentView.alengment = anlegmentInt;
}

-(void)addToolView {
    [XMKEYWINDOWXM addSubview:self.toolView];
    
}

-(void)addobserverForKeyBorad {
    @weakify(self);
    [[NSNotificationCenter defaultCenter] addObserverForName: UIKeyboardWillChangeFrameNotification object:nil queue: [NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        CGFloat endY = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y;
        CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
        CGFloat tempY = endY - 50;
        if (endY ==  SCREEN_HEIGHT) {
            tempY = SCREEN_HEIGHT;
        }
        @strongify(self);
        if (self.toolType == KeyBoard) {
            [UIView animateWithDuration:duration animations:^{
                self.toolView.y = tempY;
            }completion:^(BOOL finished) {
                [self.textColorView removeFromSuperview];
                [self.fontSelectView removeFromSuperview];
            }];
        }
    }];
    [self fontFamily];
}

-(void)fontFamily {
    NSArray * array = [UIFont familyNames];
    for (NSString * str in array) {
        NSLog(@"%@",str);
    }
}
// 结束编辑
-(void)endEidting {
    self.toolType = 0;
    [self.fontSelectView removeFromSuperview];
    [self.textColorView removeFromSuperview];
    [UIView animateWithDuration:0.25 animations:^{
        self.toolView.y = SCREEN_HEIGHT;
    }];
}
-(void)dealloc {
    NSLog(@"DIYViewModelDealloc");
    [self.toolView removeFromSuperview];
}

@end
